package com.yutel.google;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.ArrayList;

import cn.com.xinli.android.service.protocol.BoxSearch;
import cn.com.xinli.android.service.protocol.BoxSearch.PinyinSearchResp;
import cn.com.xinli.android.service.protocol.BoxSearch.SearchPacketType;

import com.yutel.pinyin.StrUtil;

public class TestBoxSearch {
	private String AUTHIPADRESS = "192.168.3.239";
	private int COLLECTIONPORT = 5405;
	private int connTimeOut = 2000;
	private int dataTimeOut = 3000;

	public static void main(String[] arg) {
		String searchUser = "35_999967343463";
		TestBoxSearch t = new TestBoxSearch();
//		t.testSearchDictReq(searchUser, "1");
//		t.testMVidSearch(searchUser, 66302);
//		t.testPinyinSearch(searchUser, "A");
		//演员２，导演３查询
		//相关推荐 分类16
//		t.testNameSearch(searchUser, "郭德纲", 3, 0, 30);
//		t.testNameSearch(searchUser, "内地剧场", 16, 0, 30);
		ArrayList<String> vids=new ArrayList<String>();
		vids.add("65552");
		vids.add("4563");
		t.testSearchReq(searchUser, vids, 0, 30);
	}

	/**
	 * 查询节目源
	 * 
	 * @param account
	 * @param channelid
	 * @param videoName
	 */
	public void testMVidSearch(String account, long vid) {
		Socket socketClient;
		DataOutputStream dataOutput;
		DataInputStream dataInput = null;
		try {
			socketClient = new Socket();
			socketClient.connect(new InetSocketAddress(AUTHIPADRESS,
					COLLECTIONPORT), connTimeOut);
			socketClient.setSoTimeout(dataTimeOut);
			dataOutput = new DataOutputStream(socketClient.getOutputStream());
			BoxSearch.MVidSearch.Builder builder = BoxSearch.MVidSearch
					.newBuilder();
			builder.setAccount(account);
			builder.setVideoid(vid);
			builder.setCount(30);
			builder.setIndex(0);
			BoxSearch.MVidSearch keywordSearch = builder.build();
			dataOutput.writeInt(BoxSearch.SearchPacketType.MVidReq.getNumber());
			dataOutput.writeInt(keywordSearch.getSerializedSize());// 传输序列化的字节长度
			dataOutput.write(keywordSearch.toByteArray());// 将发送内容转换为字节码数组
			dataOutput.flush();// 清空缓存
			dataInput = new DataInputStream(socketClient.getInputStream());
			int type = dataInput.readInt();// 获取返回值的长度
			int length = dataInput.readInt();
			System.out.println("type=" + type + ",length=" + length);
			byte dataini[] = new byte[length];// 设置长度
			dataInput.readFully(dataini);
			dataInput.close();
			socketClient.close();
			if (type == BoxSearch.SearchPacketType.SearchErr.getNumber()) {
				BoxSearch.SearchErrPacket response = BoxSearch.SearchErrPacket
						.parseFrom(dataini);
				System.out.println("error=" + response.getErrMsg());
			} else {
				BoxSearch.KeywordSearchResp response = BoxSearch.KeywordSearchResp
						.parseFrom(dataini);
				if (response != null) {
					System.out.println("ResultLeft=" + response.getResultLeft()
							+ ",ChannelType=" + response.getChannelType()
							+ ",Videoname" + response.getVideoname());
					for (int i = 0; i < response.getVideoCount(); i++) {
						BoxSearch.KeywordSearchResp.VideoInfo videoInfo = response
								.getVideo(i);
						System.out.println("SourceType="
								+ videoInfo.getSourceType());
						System.out.println(videoInfo.getValue());
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 搜索查询
	 * 
	 * @param account
	 * @param pinyin
	 */
	public void testPinyinSearch(String account, String pinyin) {
		Socket socketClient;
		DataOutputStream dataOutput;
		DataInputStream dataInput = null;
		try {
			socketClient = new Socket();
			socketClient.connect(new InetSocketAddress(AUTHIPADRESS,
					COLLECTIONPORT), connTimeOut);
			socketClient.setSoTimeout(dataTimeOut);
			dataOutput = new DataOutputStream(socketClient.getOutputStream());
			BoxSearch.PinyinSearch.Builder builder = BoxSearch.PinyinSearch
					.newBuilder();
			// builder.set
			String pinyinsearchString = StrUtil.export(pinyin);
			int searchtype = new Integer(0x10024);// 汉字搜索类型
			boolean hanzi = StrUtil.checkNameChese(pinyinsearchString);// 如果返回
																		// true说明
			// 含有汉字字符false说明为纯英文
			System.out.println("pinyinsearchString=" + pinyinsearchString
					+ ",hanzi=" + hanzi);
			builder.setAccount(account);
			builder.setIndex(0);
			builder.setCount(30);
			builder.setPinyin(pinyinsearchString);
			BoxSearch.PinyinSearch pinyinSerch = builder.build();
			System.out
					.println("BoxSearch.SearchPacketType.PinyinReq.getNumber():"
							+ BoxSearch.SearchPacketType.PinyinReq.getNumber()
							+ "");
			System.out.println("pinyinSerch.getSerializedSize():"
					+ pinyinSerch.getSerializedSize() + "");
			// 写数据
			if (hanzi) {
				dataOutput.writeInt(searchtype);
			} else {// 如果是汉字 则使用汉字 类型
				dataOutput.writeInt(BoxSearch.SearchPacketType.PinyinReq
						.getNumber());
			}
			dataOutput.writeInt(pinyinSerch.getSerializedSize());// 传输序列化的字节长度
			dataOutput.write(pinyinSerch.toByteArray());// 将发送内容转换为字节码数组
			dataOutput.flush();// 清空缓存
			System.out.println("socketClient.getReceiveBufferSize():"
					+ socketClient.getReceiveBufferSize() + "");
			dataInput = new DataInputStream(socketClient.getInputStream());
			System.out.println("GTHERE");
			int type = dataInput.readInt();// 获取返回值的长度
			int length = dataInput.readInt();
			byte dataini[] = new byte[length];// 设置长度
			dataInput.readFully(dataini);
			dataInput.close();
			socketClient.close();
			if (type == BoxSearch.SearchPacketType.SearchErr.getNumber()) {
				BoxSearch.SearchErrPacket response = BoxSearch.SearchErrPacket
						.parseFrom(dataini);
				System.out.println("error=" + response.getErrMsg());
			} else {

				BoxSearch.PinyinSearchResp response = BoxSearch.PinyinSearchResp
						.parseFrom(dataini);// 将字节流转换成对象
				if (response != null) {
					System.out.println("getNodeCount="
							+ response.getNodeCount());// 获取关键字节点个数
					System.out.println("getResultLeft="
							+ response.getResultLeft());// 获取搜索结果信息
					System.out.println("getPinyin=" + response.getPinyin());// 获取搜索拼音
					for (int i = 0; i < response.getNodeCount(); i++) {
						PinyinSearchResp.Keyword keyWord = response.getNode(i);
						System.out.print("getChannelType="
								+ keyWord.getChannelType());
						System.out.print(",getResultCount="
								+ keyWord.getResultCount());
						System.out.print(",getResultType="
								+ keyWord.getResultType());
						System.out.print(",getValue=" + keyWord.getValue());
						System.out.print(",getVid=" + keyWord.getVid());
						System.out.println(",getVideoName="
								+ keyWord.getVideoName());
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 关键字查询
	 * 
	 * @param account
	 * @param name
	 * @param type
	 * @param index
	 * @param count
	 */
	public void testNameSearch(String account, String name, int type,
			int index, int count) {
		Socket socketClient;
		DataOutputStream dataOutput;
		DataInputStream dataInput = null;
		try {
			socketClient = new Socket();
			socketClient.connect(new InetSocketAddress(AUTHIPADRESS,
					COLLECTIONPORT), connTimeOut);
			socketClient.setSoTimeout(dataTimeOut);
			dataOutput = new DataOutputStream(socketClient.getOutputStream());
			BoxSearch.NameSearch.Builder builder = BoxSearch.NameSearch
					.newBuilder();
			builder.setAccount(account);
			builder.setName(name);
			builder.setType(type);
			builder.setIndex(index);
			builder.setCount(count);
			BoxSearch.NameSearch namesearch = builder.build();
			dataOutput.writeInt(BoxSearch.SearchPacketType.NameReq.getNumber());
			dataOutput.writeInt(namesearch.getSerializedSize());// 传输序列化的字节长度
			dataOutput.write(namesearch.toByteArray());// 将发送内容转换为字节码数组
			dataOutput.flush();// 清空缓存
			dataInput = new DataInputStream(socketClient.getInputStream());
			int restype = dataInput.readInt();// 获取返回值的长度
			int length = dataInput.readInt();
			System.out.println("lenth" + length);
			System.out.println("type:" + restype);
			System.out.println("++++++++++++++=++++++++++++++++++");
			byte dataini[] = new byte[length];// 设置长度
			dataInput.readFully(dataini);
			dataInput.close();
			socketClient.close();
			if (type == BoxSearch.SearchPacketType.SearchErr.getNumber()) {
				BoxSearch.SearchErrPacket response = BoxSearch.SearchErrPacket
						.parseFrom(dataini);
				System.out.println("error=" + response.getErrMsg());
			} else {
				BoxSearch.NameSearchResp response = BoxSearch.NameSearchResp
						.parseFrom(dataini);
				if (response != null) {
					System.out.println("getType=" + response.getType());
					System.out.println("getResultLeft="
							+ response.getResultLeft());
					System.out.println("getValue=" + response.getValue());
					for (int i = 0; i < response.getVideosCount(); i++) {
						BoxSearch.NameSearchResp.Video video = response
								.getVideos(i);
						System.out
								.print("getVideoName=" + video.getVideoName());
						System.out.print(",getVid=" + video.getVid());
						System.out.print(",getPoster=" + video.getPoster());
						System.out.print(",getChannelType="
								+ video.getChannelType());
						for (int j = 0; j < video.getCategoryTypeCount(); j++) {
							System.out.print(":getCategoryType="
									+ video.getCategoryType(j));
						}
						System.out.println();
					}
				}
			}
			System.out.println("++++++++++++++=++++++++++++++++++");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 查询影片详情
	 * 
	 * @param account
	 * @param version
	 */
	public void testSearchReq(String account, ArrayList<String> vids, int start,
			int count) {
		Socket socketClient;
		DataOutputStream dataOutput;
		DataInputStream dataInput = null;
		try {

			socketClient = new Socket();
			socketClient.connect(new InetSocketAddress(AUTHIPADRESS,
					COLLECTIONPORT), connTimeOut);
			socketClient.setSoTimeout(dataTimeOut);
			dataOutput = new DataOutputStream(socketClient.getOutputStream());
			BoxSearch.SearchReq.Builder builder = BoxSearch.SearchReq
					.newBuilder();
			builder.setAccount(account);
			builder.setStart(start);
			builder.setCount(count);
			builder.setSearchType(0x20001);// 固定类型
			for(String vid: vids) {
				BoxSearch.Attribute.Builder att = BoxSearch.Attribute.newBuilder();
				att.setType(0x10009);// 固定数值
				att.setValue(vid);
				builder.addFilter(att);
			}
			BoxSearch.SearchReq searchReq = builder.build();
			dataOutput.writeInt(SearchPacketType.NormalReq.getNumber());
			dataOutput.writeInt(searchReq.getSerializedSize());// 传输序列化的字节长度
			dataOutput.write(searchReq.toByteArray());// 将发送内容转换为字节码数组
			dataOutput.flush();// 清空缓存
			dataInput = new DataInputStream(socketClient.getInputStream());
			int type = dataInput.readInt();// 获取返回值的长度
			int length = dataInput.readInt();
			System.out.println("++++++++++++++=++++++++++++++++++");
			System.out.println("type=" + type + ",length=" + length);
			byte dataini[] = new byte[length];// 设置长度
			dataInput.readFully(dataini);
			dataInput.close();
			socketClient.close();
			if (type == BoxSearch.SearchPacketType.SearchErr.getNumber()) {
				BoxSearch.SearchErrPacket response = BoxSearch.SearchErrPacket
						.parseFrom(dataini);
				System.out.println("response=" + response.getErrMsg());
			} else {
				BoxSearch.SearchResp response = BoxSearch.SearchResp
						.parseFrom(dataini);
				if (response != null) {
					System.out
							.println("ResultLeft=" + response.getResultLeft());
					System.out
							.println("SearchType=" + response.getSearchType());

					for (int i = 0; i < response.getResultCount(); i++) {
						System.out.println("Type\tValue");
						BoxSearch.SearchResult result = response.getResult(i);
						for (int j = 0; j < result.getAttrCount(); j++) {
							BoxSearch.Attribute attribute = result.getAttr(j);
							System.out.println(attribute.getType() + "\t"
									+ attribute.getValue());
						}
					}
				}
			}
			System.out.println("++++++++++++++=++++++++++++++++++");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 查询数据字典
	 * 
	 * @param account
	 * @param version
	 */
	public void testSearchDictReq(String account, String version) {
		Socket socketClient;
		DataOutputStream dataOutput;
		DataInputStream dataInput = null;
		try {

			socketClient = new Socket();
			socketClient.connect(new InetSocketAddress(AUTHIPADRESS,
					COLLECTIONPORT), connTimeOut);
			socketClient.setSoTimeout(dataTimeOut);
			dataOutput = new DataOutputStream(socketClient.getOutputStream());
			BoxSearch.SearchDictReq.Builder builder = BoxSearch.SearchDictReq
					.newBuilder();
			builder.setAccount(account);
			builder.setVersion(version);
			BoxSearch.SearchDictReq searchDicReq = builder.build();
			dataOutput.writeInt(BoxSearch.SearchPacketType.DictReq.getNumber());
			dataOutput.writeInt(searchDicReq.getSerializedSize());// 传输序列化的字节长度
			dataOutput.write(searchDicReq.toByteArray());// 将发送内容转换为字节码数组
			dataOutput.flush();// 清空缓存
			dataInput = new DataInputStream(socketClient.getInputStream());
			int type = dataInput.readInt();// 获取返回值的长度
			int length = dataInput.readInt();
			System.out.println("++++++++++++++=++++++++++++++++++");
			System.out.println("type=" + type + ",length=" + length);
			byte dataini[] = new byte[length];// 设置长度
			dataInput.readFully(dataini);
			dataInput.close();
			socketClient.close();
			if (type == BoxSearch.SearchPacketType.SearchErr.getNumber()) {
				BoxSearch.SearchErrPacket response = BoxSearch.SearchErrPacket
						.parseFrom(dataini);
				System.out.println("response=" + response.getErrMsg());
			}
			BoxSearch.SearchDictResp response = BoxSearch.SearchDictResp
					.parseFrom(dataini);
			if (response != null) {
				System.out.println("ResultLeft=" + response.getResultLeft());
				System.out.println("DictTypeCount="
						+ response.getDictTypeCount());
				System.out.println("Num\tType\tValue");
				for (int i = 0; i < response.getDictCount(); i++) {
					BoxSearch.SearchDict dict = response.getDict(i);
					System.out.println(dict.getNum() + "\t" + dict.getType()
							+ "\t" + dict.getValue());
				}
			}
			System.out.println("++++++++++++++=++++++++++++++++++");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}